﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.model.Instance;

namespace l1cs.server.serverpackets
{
    public class S_IdentifyDesc : ServerBasePacket
    {
        private static Logger _log = Logger.getLogger( typeof( S_IdentifyDesc ).FullName );

        private byte[] _byte = null;

        /**
         * 確認スクロール使用時のメッセージを表示する
         */
        public S_IdentifyDesc( L1ItemInstance item )
        {
            buildPacket( item );
        }

        private void buildPacket( L1ItemInstance item )
        {
            writeC( Opcodes.S_OPCODE_IDENTIFYDESC );
            writeH( item.getItem().getItemDescId() );

            StringBuilder name = new StringBuilder();

            if ( item.getItem().getBless() == 0 )
            {
                name.Append( "$227 " ); // 祝福された
            }
            else if ( item.getItem().getBless() == 2 )
            {
                name.Append( "$228 " ); // 呪われた
            }

            name.Append( item.getItem().getIdentifiedNameId() );

            if ( item.getItem().getType2() == 1 )
            { // weapon
                writeH( 134 ); // \f1%0：小さなモンスター打撃%1 大きなモンスター打撃%2
                writeC( 3 );
                writeS( name.ToString() );
                writeS( item.getItem().getDmgSmall()
                        + "+" + item.getEnchantLevel() );
                writeS( item.getItem().getDmgLarge()
                        + "+" + item.getEnchantLevel() );

            }
            else if ( item.getItem().getType2() == 2 )
            { // armor
                if ( item.getItem().getItemId() == 20383 )
                { // 騎馬用ヘルム
                    writeH( 137 ); // \f1%0：使用可能回数%1［重さ%2］
                    writeC( 3 );
                    writeS( name.ToString() );
                    writeS( Convert.ToString( item.getChargeCount() ) );
                }
                else
                {
                    writeH( 135 ); // \f1%0：防御力%1 防御具
                    writeC( 2 );
                    writeS( name.ToString() );
                    writeS( Math.Abs( item.getItem().get_ac() )
                            + "+" + item.getEnchantLevel() );
                }

            }
            else if ( item.getItem().getType2() == 0 )
            { // etcitem
                if ( item.getItem().getType() == 1 )
                { // wand
                    writeH( 137 ); // \f1%0：使用可能回数%1［重さ%2］
                    writeC( 3 );
                    writeS( name.ToString() );
                    writeS( Convert.ToString( item.getChargeCount() ) );
                }
                else if ( item.getItem().getType() == 2 )
                { // light系アイテム
                    writeH( 138 );
                    writeC( 2 );
                    name.Append( ": $231 " ); // 残りの燃料
                    name.Append( Convert.ToString( item.getRemainingTime() ) );
                    writeS( name.ToString() );
                }
                else if ( item.getItem().getType() == 7 )
                { // food
                    writeH( 136 ); // \f1%0：満腹度%1［重さ%2］
                    writeC( 3 );
                    writeS( name.ToString() );
                    writeS( Convert.ToString( item.getItem().getFoodVolume() ) );
                }
                else
                {
                    writeH( 138 ); // \f1%0：［重さ%1］
                    writeC( 2 );
                    writeS( name.ToString() );
                }
                writeS( Convert.ToString( item.getWeight() ) );
            }
        }

        override
        public byte[] getContent()
        {
            if ( _byte == null )
            {
                _byte = getBytes();
            }
            return _byte;
        }

        override public String getType()
        {
            return "[S] S_IdentifyDesc";
        }
    }
}